$(function () {
    if($.cookie('Second')){
        var Second = JSON.parse($.cookie('Second'))
        $('input[name="img"]').val(Second.img)
        $("#icon").attr('src',services+Second.img);
        $('input[name="name"]').val(Second.name)
        var gender  = Second.gender;
        if(gender==1){
            $("#male").click();
        }else{
            $("#female").prop("checked",true);
        }
        $("input[name='birthday']").val(Second.birthday)
        $("input[name='firstWorkTime']").val(Second.firstWorkTime)
    }

    
    useLayUI();
    $("#infobutton").click(function(){
        var img = $('input[name="img"]').val()
        var name = $('input[name="name"]').val()
        var gender = $('input:radio[name="gender"]:checked').val()
        var birthday = $('input[name="birthday"]').val()
        var firstWorkTime = $('input[name="firstWorkTime"]').val()
        if(name==""||name==null){
            layerMsg("名字不为空")
            return false;
        }
        if(gender==""||gender==null){
            layerMsg("性别不为空")
            return false;
        }
        if(birthday==""||birthday==null){
            layerMsg("出生日期不为空")
            return false;
        }
        $.ajax({
            type: 'post', // 提交方式 get/post
            url: services+"/user/reg", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
				withCredentials: true
			}
            ,crossDomain: true
            ,data:JSON.stringify({
                "name":name				
                ,"gender":gender			
                ,"birthday":birthday			
                ,"firstWorkTime":firstWorkTime	
                ,"img":img		
            }),
             success: function(res) {
                if(res.success){
                    data = res.data;
                    $.cookie('userInfo',JSON.stringify(data))
                    layerMsg(res.message);
                    setTimeout(function() {
                        $.cookie('Second',{ expires: -1 })
                        $.cookie('oneStep',{ expires: -1 })
                        window.location.href="register-work.html" ;
                    },3000);
                    
                }else{
                    layerMsg(res.message)
                }               
            },
        })
    });

})
//上传图片
function uploadImg(str){

    var fileSize = $('#file')[0].files[0].size;
    var size = fileSize / 1024;
    if(size>5000){
        layerMsg("图片不超于5M");
       return false;
    }
    layer.load(2);


    var file = $('#file').val();
    var option = {
        type: 'post', // 提交方式 get/post
        url: services+"/user/upload/img", // 需要提交的 url
        data: {
        'file': file,
         },
         success: function(res) {
            layer.closeAll('loading');
            var obj = JSON.parse(res);
            if(obj.code==0){
             //    console.log(obj.data)
                src = services+obj.data.src;
                $('#'+str).attr('src',src);
                $('#img').val(obj.data.src) ;
                layerMsg(obj.msg);
            }
        }
    }
    $("#uploadImgform").ajaxSubmit(option);
        
}

function ret(){
    var img = $('input[name="img"]').val()
    var name = $('input[name="name"]').val()
    var gender = $('input:radio[name="gender"]:checked').val()
    var birthday = $('input[name="birthday"]').val()
    var firstWorkTime = $('input[name="firstWorkTime"]').val()
    var Second = {
        'img':img,
        'name':name,
        'gender':gender,
        'birthday':birthday,
        'firstWorkTime':firstWorkTime
    }
    // console.log(typeof(Second))
    $.cookie('Second',JSON.stringify(Second))
    // console.log($.cookie('Second'));
    window.location.href="../register-set.html" ;
    // ../register-set.html
}